/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.service;

import br.com.foxline.enums.Reports;
import br.com.foxline.model.*;
import br.com.foxline.util.ReportService;
import br.com.foxline.util.VOAnnex;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author roniere
 */
@Stateless
@LocalBean
public class AnnexService extends GenericService<Annex> {

    public AnnexService() {
        super(Annex.class);
    }

    public List<Annex> getAnnexByCharter(Charter charter) {
        String sql = "Select a from Charter c JOIN c.annex a where c.id = :id";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", charter.getId());

        return query.getResultList();
    }

    public List<Annex> getAnnexByCharter(CharterSDI charter) {
        String sql = "Select a from CharterSDI c JOIN c.annex a where c.id = :id";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", charter.getId());

        return query.getResultList();
    }

    public List<Annex> getAnnexByCharter(CharterTestament charter) {
        String sql = "Select a from CharterTestament c JOIN c.annex a where c.id = :id";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", charter.getId());

        return query.getResultList();
    }

    public List<Annex> getAnnexByTitleDocument(TitleDocument titleDocument) {
        String sql = "Select a from TitleDocument c JOIN c.annex a where c.id = :id";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", titleDocument.getId());

        return query.getResultList();
    }

    public void genReportAnnex(Annex annex, HttpServletRequest request, HttpServletResponse response) {
        
        HashMap<String, Object> parametros;
        parametros = new HashMap<String, Object>();
        InputStream imgInputStream = null;

        imgInputStream = new ByteArrayInputStream(annex.getFileByte());
        List<VOAnnex> annexs = new ArrayList<VOAnnex>();
        
        annexs.add(new VOAnnex(imgInputStream));
        
        ReportService.genReport(parametros, annexs, Reports.ANNEX, request, response);
        
    }
}
